let axios = require('axios')
let db = require('./db/sqlite.js')
let fs = require('fs')
let task = require('./task')
let history = require('./history')

class V {
  constructor() {

  }
  /**
   * 生成指定单位数数组，用于统计
   */
  async formatTail() {
    let list = await history.query({
      order: 'order by issue_number'
    })
    let tailList = []
    list.forEach(item => {
      if (item.a < 10) {
        tailList.push(item.a) //16
        // tailList.push(item.b)//16
        // tailList.push(item.c)//17
        // tailList.push(item.d)//22
        // tailList.push(item.e)//21
      }

    })
    fs.writeFileSync('./dist/tail.json', JSON.stringify(tailList))
  }

  date() {
    return {
      addDate(date, days) {
        if (days == undefined || days == '') {
          days = 1;
        }
        var date = new Date(date);
        date.setDate(date.getDate() + days);
        var month = date.getMonth() + 1;
        var day = date.getDate();
        return date.getFullYear() + '-' + this.getFormatDate(month) + '-' + this.getFormatDate(day);

      },
      getFormatDate(arg) {
        if (arg == undefined || arg == '') {
          return '';
        }

        var re = arg + '';
        if (re.length < 2) {
          re = '0' + re;
        }

        return re;
      }
    }
  }

  /**
   * 检查数据库确实数据,哪一期没有
   */
  async checkData(date = '2015-07-01') {
    let count = 0;
    let arr = []
    for (let i = 0; i < 13600; i++) {
      date = this.date().addDate(date, 1);
      for (let j = 1; j <= 120; j++) {
        let issue_number = parseInt(`${date.replace(/\-/g,'')}${j>99?j:j>9?('0'+j):('00'+j)}`)
        let result = await history.query({
          where: ` and issue_number=${issue_number}`
        })
        if (result.length === 0) {
          count++
          arr.push(issue_number)
          // issue_number++
          // if (parseInt((issue_number+'').match(/\d{3}$/)[0]) <= 120) {
          //   let issueno = (issue_number + '').replace(/\d{3}$/, val => {
          //     return `-${val}`
          //   })
          //   await history.getData(0, issueno, 1)
          // }
        }
        console.log(count)
      }

      if (new Date(date).getTime() > new Date('2019-02-02').getTime()) {
        fs.writeFileSync('./dist/yl.json', JSON.stringify(arr))
        break
      }
    }
  }
  /**
   * 寻找万千百最大组合，遗漏期最长
   * 生成全部组合，存到数据库里面
   */
  async group() {
    console.log('>>>test1')
    //-----------
    // let sql = `select * from (SELECT * from ssc_history
    //   order by issue_number desc
    //   limit 240) a
    //   order by a.issue_number`
    // let list = await history.queryData(sql)
    // //-----------
    // let content = fs.readFileSync('./dist/formatArr.json')
    // let arr = JSON.parse(content)
    // let groupArr = []
    // for (let i = 0; i < arr.length; i++) {
    //   for (let j = 0; j < arr.length; j++) {
    //     for (let z = 0; z < arr.length; z++) {
    //       groupArr.push([
    //         arr[i], arr[j], arr[z]
    //       ])
    //     }
    //   }
    // }
    // //-------------------
    // console.log('>>>去重')
    // list.forEach((item, index) => {
    //   console.log(JSON.stringify(item))
    //   console.log(groupArr.length)
    //   for (var i = 0; i < groupArr.length; i++) {
    //     if (groupArr[i][0].indexOf(item.a) > -1 && groupArr[i][1].indexOf(item.b) > -1 && groupArr[i][2].indexOf(item.c) > -1) {
    //       groupArr.splice(i, 1)
    //       i--
    //     }
    //   }
    //   console.log(index, groupArr.length)
    // })
    // [1, 1, 1]
    // [2, 5, 6]
    // [6, 8, 3]
    // [1, 3, 0]
    // [2, 4, 0]
    // [9, 5, 7]
    let arr = []
    let start = 0;
    let end = 9
    //1位
    for (let i = start; i <= end; i++) {
      console.log(i)
      arr.push(`${i}`)

    }
    //2位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        arr.push(`${i}${j}`)
      }
    }
    //3位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          arr.push(`${i}${j}${z}`)
        }
      }
    }
    //4位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            arr.push(`${i}${j}${z}${a}`)
          }
        }
      }
    }
    //5位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            for (let b = a + 1; b <= end; b++) {
              arr.push(`${i}${j}${z}${a}${b}`)
            }
          }
        }
      }
    }
    //6位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            for (let b = a + 1; b <= end; b++) {
              for (let c = b + 1; c <= end; c++) {
                arr.push(`${i}${j}${z}${a}${b}${c}`)
              }
            }
          }
        }
      }
    }
    //7位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            for (let b = a + 1; b <= end; b++) {
              for (let c = b + 1; c <= end; c++) {
                for (let d = c + 1; d <= end; d++) {
                  arr.push(`${i}${j}${z}${a}${b}${c}${d}`)
                }
              }
            }
          }
        }
      }
    }
    //8位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            for (let b = a + 1; b <= end; b++) {
              for (let c = b + 1; c <= end; c++) {
                for (let d = c + 1; d <= end; d++) {
                  for (let e = d + 1; e <= end; e++) {
                    arr.push(`${i}${j}${z}${a}${b}${c}${d}${e}`)
                  }
                }
              }
            }
          }
        }
      }
    }
    //9位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            for (let b = a + 1; b <= end; b++) {
              for (let c = b + 1; c <= end; c++) {
                for (let d = c + 1; d <= end; d++) {
                  for (let e = d + 1; e <= end; e++) {
                    for (let f = e + 1; f <= end; f++) {
                      arr.push(`${i}${j}${z}${a}${b}${c}${d}${e}${f}`)
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    //10位
    for (let i = start; i <= end; i++) {
      for (let j = i + 1; j <= end; j++) {
        for (let z = j + 1; z <= end; z++) {
          for (let a = z + 1; a <= end; a++) {
            for (let b = a + 1; b <= end; b++) {
              for (let c = b + 1; c <= end; c++) {
                for (let d = c + 1; d <= end; d++) {
                  for (let e = d + 1; e <= end; e++) {
                    for (let f = e + 1; f <= end; f++) {
                      for (let g = f + 1; g <= end; g++) {
                        arr.push(`${i}${j}${z}${a}${b}${c}${d}${e}${f}${g}`)
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    console.log(arr.length)
    let newArr = []
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < arr.length; j++) {
        for (let y = 0; y < arr.length; y++) {
          console.log(`i:${i}|j:${j}|y:${y}`)
          //写入数据库
          await this.guropInsert([arr[i], arr[j], arr[y], `${arr[i]}|${arr[j]}|${arr[y]}`])
        }
      }
    }
    console.log(newArr.length)
  }

  guropInsert(data) {
    return new Promise(async (resolve, reject) => {
      try {
        console.log('>>>insert', JSON.stringify(data))
        let sql = `INSERT INTO ssc_group (a,b,c,k1) VALUES ('${data[0]}','${data[1]}','${data[2]}','${data[3]}')`
        console.log(sql)
        history.db.executeSql(sql, res => {
          resolve()
        })
      } catch (err) {
        console.error(err)
        reject(err)
      }
    })
  }
  /**
   * 生成不重复组合
   */
  createCombination() {
    console.log('>>>createCombination')

    function isRepeat(numbuer) {
      let flag = false;
      let ary = (numbuer + '').split('');
      let nary = ary.slice().sort();
      for (let i = 0; i < ary.length; i++) {
        if (nary[i] == nary[i + 1]) {
          flag = true;
          break;
        }
      }
      return flag;
    }
    let arr = []
    for (let i = 10000; i <= 43210; i++) {
      if (!isRepeat(i)) {
        let newNumber = (i + '').split('').slice().sort().toString().replace(/,/g, '')
        if (arr.indexOf(newNumber) === -1) {
          arr.push(newNumber)
        }
      }
    }
    console.log(arr.length)
    fs.writeFileSync('./dist/formatArr.json', JSON.stringify(arr))
  }
  test2() {

    // var value = 0,
    //   temp1 = 0,
    //   temp2 = 0,
    //   temp3 = 0,
    //   temp4 = 0,
    //   temp5 = 0,
    //   j = 0;
    // var start = new Date().getTime();
    // for (var i = 0; i < 10000000000; i++ ) {
    //   temp1 += i * j;
    //   temp2 += i;
    //   temp3 += j;
    //   temp4 += i * i;
    //   temp5 += j * j;
    // }

    // var end = new Date().getTime();
    // console.log(end - start);

    // function fibonacci(i) {
    //   if (i < 2) return i;
    //   return fibonacci(i - 2) + fibonacci(i - 1);
    // }
    // console.log(fibonacci(340))
  }

  async main() {
    console.log('>>>>start')
    // await this.formatTail()
    // await task.algorithm2(8, 80); //遗漏期数，最大倍投数
    // await this.checkData('2016-01-01')
    // history.thread(112500, 5)
    // this.createCombination()
    await this.group()
    console.log('>>>>end')

  }
}

new V().main()
